<html>
<head><meta charset="utf-8"><title>Simplifying continue-with-unit in `ControlFlow` · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Simplifying.20continue-with-unit.20in.20.60ControlFlow.60.html">Simplifying continue-with-unit in `ControlFlow`</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="209151522"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Simplifying%20continue-with-unit%20in%20%60ControlFlow%60/near/209151522" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Simplifying.20continue-with-unit.20in.20.60ControlFlow.60.html#209151522">(Sep 05 2020 at 00:04)</a>:</h4>
<p>(Context: <a href="https://doc.rust-lang.org/nightly/std/ops/enum.ControlFlow.html"><code>ControlFlow</code></a>, tracking issue <a href="https://github.com/rust-lang/rust/issues/75744">#75744</a>)</p>
<p>ecstatic-morse correctly pointed out that <code>Continue</code> not carrying a value is the majority use -- like how <a href="https://github.com/rust-lang/rust/blob/1.46.0/src/libcore/iter/traits/iterator.rs#L2227"><code>Iterator::find</code> just <code>Continue(())</code>s</a>, and <a href="https://github.com/rust-lang/rust/blob/1.46.0/src/librustc_data_structures/graph/iterate/mod.rs#L89-L94">rustc's version didn't even support a value</a> -- so I wanted to have a conversation about how (and whether) to make it easier to continue-with-<code>()</code> via this type.  (<code>Continue</code> with a non-unit value is important for <code>try_fold</code>, so I wouldn't want to remove it entirely.)</p>
<p>The simplest version of it is just <code>const CONTINUE = ControlFlow::Continue(());</code>.  That follows the naming conventions, works fine in patterns, etc.  It does aesthetically stand out to me in use when used because of the SHOUTING, though.  (I'd <a href="https://github.com/rust-lang/rust/blob/59e37332b0d2d0c5e43bea1184ec0387fb07cc80/library/core/src/ops/control_flow.rs#L70-L88">done this one</a> for now, as it's easy to delete later if it proves bad.)</p>
<p>A more "cute" version would be something like this:</p>
<div class="codehilite"><pre><span></span><code><span class="k">enum</span> <span class="nc">ControlFlow</span><span class="o">&lt;</span><span class="n">B</span><span class="p">,</span><span class="w"> </span><span class="n">C</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">BreakWith</span><span class="p">(</span><span class="n">B</span><span class="p">),</span><span class="w"> </span><span class="n">ContinueWith</span><span class="p">(</span><span class="n">C</span><span class="p">)</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="k">impl</span><span class="w"> </span><span class="n">ControlFlow</span><span class="o">&lt;</span><span class="n">B</span><span class="p">,</span><span class="w"> </span><span class="p">()</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">Continue</span>: <span class="nc">Self</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ControlFlow</span>::<span class="n">Continue</span><span class="p">(());</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>That more closely looks like the normal <code>continue;</code>, but I don't know if faking a variant using a const like that is stylistically acceptable.  Similarly, <code>BreakWith(4)</code> is a bit more conversational of a name than seems normal for the library.  I guess it could instead be <code>BreakValue(4)</code> or something, like the "loop break value" feature name?</p>
<p>Of course, there's always the option of just doing nothing, since we've also done nothing for its isomorphic cousin <code>Ok(())</code>.  It may well be that this type itself, while important, is niche enough in use that the <code>(())</code> isn't a problem at all.  Certainly if we were to get <code>try_blocks</code> one day, <code>ControlFlow::Continue(())</code> could just be spelled <code>try{}</code> -- even shorter than <code>CONTINUE</code>.</p>
<p>(I guess there are also other options like <code>r#break!()</code>, but a macro here seems obviously out of place.)</p>
<p>Thoughts?</p>



<a name="209151883"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Simplifying%20continue-with-unit%20in%20%60ControlFlow%60/near/209151883" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Simplifying.20continue-with-unit.20in.20.60ControlFlow.60.html#209151883">(Sep 05 2020 at 00:13)</a>:</h4>
<p>Probably not worth doing now, but mapping keyword continue and break to these seems like the right fit long term, perhaps.</p>
<p>I've personally felt that the SCREAMING default for constants isn't great for a lot of cases like this one.</p>
<p>I think I'd lean towards an associated function constructor, or nothing</p>



<a name="209152209"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Simplifying%20continue-with-unit%20in%20%60ControlFlow%60/near/209152209" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Simplifying.20continue-with-unit.20in.20.60ControlFlow.60.html#209152209">(Sep 05 2020 at 00:21)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116122">simulacrum</span> <a href="#narrow/stream/219381-t-libs/topic/Simplifying.20continue-with-unit.20in.20.60ControlFlow.60/near/209151883">said</a>:</p>
<blockquote>
<p>I think I'd lean towards an associated function constructor, or nothing</p>
</blockquote>
<p>Hmm, can you elaborate on that one?  I'm not sure I follow.  Unless maybe you're thinking something like <code>ControlFlow::break_if(x &lt; 0)</code> to avoid mentioning the variants explicitly at all?</p>



<a name="209156832"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Simplifying%20continue-with-unit%20in%20%60ControlFlow%60/near/209156832" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Simplifying.20continue-with-unit.20in.20.60ControlFlow.60.html#209156832">(Sep 05 2020 at 02:21)</a>:</h4>
<p>No, I meant <code>ControlFlow::continue()</code>. I think break_if isn't bad though</p>



<a name="209156838"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Simplifying%20continue-with-unit%20in%20%60ControlFlow%60/near/209156838" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Simplifying.20continue-with-unit.20in.20.60ControlFlow.60.html#209156838">(Sep 05 2020 at 02:21)</a>:</h4>
<p>(with must_use, anyway, since otherwise could be really confusing)</p>



<a name="209156917"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Simplifying%20continue-with-unit%20in%20%60ControlFlow%60/near/209156917" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Simplifying.20continue-with-unit.20in.20.60ControlFlow.60.html#209156917">(Sep 05 2020 at 02:24)</a>:</h4>
<p>I think faking the variant isn't necessarily bad but does sort of feel odd at least</p>



<a name="209158420"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Simplifying%20continue-with-unit%20in%20%60ControlFlow%60/near/209158420" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Simplifying.20continue-with-unit.20in.20.60ControlFlow.60.html#209158420">(Sep 05 2020 at 03:05)</a>:</h4>
<p>isn't that a keyword? <code>ControlFlow::r#continue()</code>?</p>



<a name="209160201"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Simplifying%20continue-with-unit%20in%20%60ControlFlow%60/near/209160201" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Simplifying.20continue-with-unit.20in.20.60ControlFlow.60.html#209160201">(Sep 05 2020 at 04:00)</a>:</h4>
<p>A little off-topic: Sometimes i feel that there's a need for a macro that can convert a <code>ControlFlow</code> value into real "actions", <code>break</code>, <code>continue</code>, <code>return</code>, <code>await</code> etc.  This will help solve the "closures cannot easily mutate control flow" issue by executing this macro on its return value...</p>



<a name="209175286"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Simplifying%20continue-with-unit%20in%20%60ControlFlow%60/near/209175286" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Simplifying.20continue-with-unit.20in.20.60ControlFlow.60.html#209175286">(Sep 05 2020 at 12:14)</a>:</h4>
<p>Hm, yeah it probably is a keyword.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>